#include "stdafx.h"
#include "Render.h"
#include "IClientRoot.h"
using namespace Apo;

Render::Render(void)
:m_OgreRoot(NULL)
,m_SceneManager(0)
,m_Window(0)
{
}

Render::~Render(void)
{
}

void Render::start(HWND phWnd,unsigned int width, unsigned int height,void * p)
{
	std::string rName ;
	try
	{
		m_OgreRoot = OGRE_NEW Ogre::Root("plugins.cfg", "Ogre.cfg", "Ogitor.log"); 
	

	ConfigFile cf;
	cf.load("resources.cfg");

	// Go through all sections & settings in the file
	ConfigFile::SectionIterator seci = cf.getSectionIterator();

	std::string secName, typeName, archName;
	while (seci.hasMoreElements())
	{
		secName = seci.peekNextKey();
		ConfigFile::SettingsMultiMap *settings = seci.getNext();
		ConfigFile::SettingsMultiMap::iterator i;
		for (i = settings->begin(); i != settings->end(); ++i)
		{
			typeName = i->first;
			archName = i->second;
			ResourceGroupManager::getSingleton().addResourceLocation(
				archName, typeName, secName);
		}
	}

	
	// Look though the list of rendering systems and find the renderer that 
	// we're going to use.
	
	// Note:  To use OpenGL renderer, just change the line to:
	    if (rName == "OpenGL Rendering Subsystem")
	 

	 		LogManager::getSingleton().setLogDetail(LL_BOREME);
	 
	 #if ((OGRE_VERSION_MAJOR * 10) + OGRE_VERSION_MINOR) >= 17
	 		RenderSystemList::const_iterator pRend = m_OgreRoot->getAvailableRenderers().begin();
	 		while (pRend != m_OgreRoot->getAvailableRenderers().end())
	 #else
	 		RenderSystemList::const_iterator pRend = m_OgreRoot->getAvailableRenderers()->begin();
	 		while (pRend != m_OgreRoot->getAvailableRenderers()->end())
	 #endif
	 		{
	 			std::string rName = (*pRend)->getName();
	 			if (rName == "Direct3D9 Rendering Subsystem")
	 				break;
	 			pRend++;
	 		}
	 
	 #if ((OGRE_VERSION_MAJOR * 10) + OGRE_VERSION_MINOR) >= 17
	 		if (pRend == m_OgreRoot->getAvailableRenderers().end())
	 #else
	 		if (pRend == m_OgreRoot->getAvailableRenderers()->end())
	 #endif
	 		{
	 			// Unrecognised render system
					MessageBoxA(NULL, "Unable to locate rendering system.  Application is terminating", NULL, MB_OK);
	 			//MessageBoxA(L"Unable to locate rendering system.  Application is terminating");
	 			return ;
	 		}
	 
	 		RenderSystem *rsys = *pRend;
	 
	 		// Some standard rendering system configurations
	 		rsys->setConfigOption("Full Screen", "No");
	 		rsys->setConfigOption("VSync", "No");
	 
	 		// Set the rendering system and Initialise OGRE
	 		m_OgreRoot->setRenderSystem(rsys);
	 		m_OgreRoot->initialise(false);
	 		MovableObject::setDefaultQueryFlags(0);

			SetupOgre( phWnd, width,  height);

	}
	catch (Exception &e)
	{
		MessageBoxA(NULL, e.getFullDescription().c_str(), NULL, MB_OK);
	}


	 
}
void Render::SetupOgre(HWND phWnd ,unsigned int width, unsigned int height)
{
	RECT   rect;
	GetClientRect(phWnd,&rect);
	width = rect.right - rect.left;
	height = rect.bottom- rect.top;
	//m_OgreRoot = theApp.GetOgreRoot();

	NameValuePairList parms;
	parms["externalWindowHandle"] = StringConverter::toString((long)phWnd);
	m_Window = m_OgreRoot->createRenderWindow("Apo Window", width, height, false, &parms);
	Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);

	// Load resources

	ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

	//m_OgreRoot->getRenderSystem()->addListener(this);
	//m_OgreRoot->SetRenderWindow(m_Window);
	//m_OgreInitialised = true;
	m_SceneManager = m_OgreRoot->createSceneManager("OctreeSceneManager");

}

bool Render::frameStarted(const FrameEvent& evt)
{ 
	(void)evt; return true;
}


void Render::stop()
{

}

void Render::updata(float time) 
{
// 	CPaintDC dc(this); // device context for painting
// 	CString msgstr = _T("Initializing OGRE...");
// 	CRect rc;
// 	GetClientRect(&rc);
// 	Ogre::String a;

// 	if(m_OgreInitialised&&theApp.m_OgitorsRoot->IsSceneLoaded()&&!m_RenderStop)
// 	{
// 		if(rc.Width() > 0 && rc.Height() > 0)
// 		{
// 			if(((CMainFrame*)theApp.GetMainWnd())->m_wndAddEntity.GetUpdateNeeded())
// 			{
// 				((CMainFrame*)theApp.GetMainWnd())->m_wndAddEntity.GetRenderWindow()->setActive(true);
// 			}

// 			if(ClientRoot::getSingletonPtr()->IsClearScreenNeeded())
// 			{
// 				Ogre::Camera *clearcam = ClientRoot::getSingletonPtr()->GetSceneManager()->createCamera("sbtClearCam");
// 				clearcam->setNearClipDistance(0.0001f);
// 				clearcam->setFarClipDistance(0.0002f);
// 				clearcam->lookAt(0,-1,0);
// 				ClientRoot::getSingletonPtr()->GetRenderWindow()->addViewport(clearcam,0);
// 			}

	try
	{
		m_OgreRoot->renderOneFrame();

	}
	catch (Ogre::Exception& e)
	{
		MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_ICONERROR | MB_TASKMODAL);
	}


}

Render  *gRender;
void  dllStartPlugin(void)
{
	gRender= new Render();
	Apo::IClientRoot::getIClientRoot()->RegisterRender(gRender);
}

void  dllStopPlugin(void)
{
  delete gRender;
}
